<?php

namespace View;

class PostView
{
	//Strängplatser för $_POST
	private $getHeader = "Header";
	private $getTopicHeader = "TopicHeader";
	private $getContent = "Content";
	private $getCreateKey = "CreateKey";		
	private $getDeletePost = "DeletePost";	
	private $getPostId = "PostId";	
	
	//Konstanter
	const NEW_POST_HEADER = 1;
	const MY_POSTS_HEADER = 2;
	const WAS_LOGGED_OUT = 3;
	const NO_POSTS = 4;
	
	//Retunerar rubriker via konstantanrop
	public function RenderHeader($header)
	{
		switch($header)
		{	
			case self::MY_POSTS_HEADER;
				return "<div class='contentHeader'><h2>My posts</h2></div>";
				break;																
		}			
	}	

	//Retunerar meddelanden via konstantanrop	
	public function RenderMessage($message)
	{
		switch($message)
		{
			case self::WAS_LOGGED_OUT;
				return "<p class='msg'>You are no longer logged in. Please login to continue.</p>";
				break;
			case self::NO_POSTS;
				return "<p class='msg'>You have not made any posts</p>";
				break;				
		}
	}

	//Retunerar felmeddelanden via array-anrop
	public function RenderErrorArray($errorArray)
	{
		$ret = "<div class='errorMsg'>";
		
		foreach($errorArray as $error)
		{
			$ret .= "<p>" . $error . "</p>";
		}
		
		$ret .= "</div>";					
		return $ret;		
	}		
	
	//Retunerar lista av Post-objekt via medskickad array.
	//Retunerar Deleteknapp efter de post-objekt som är skapade av samma id som skickas med vid anrop		
	public function RenderPostList(\Model\PostArray $postArray, $loggedInUserId)
	{
		$ret = NULL;
					
		foreach ($postArray->Get() as $post)
		{
			$ret .= "<div class='postDiv'><p class='nameAndDate'>Re:" . $post->GetTopicHeader() . " by " . $post->GetUserName() . " " . $post->GetDate() . "</p>
					 <p class='postContent'>" . $post->GetContent() . "</p></div>";

			if($post->GetUserId() == $loggedInUserId)
			{
				$ret .= "<form class='deleteForm' method='post'>";
				$ret .= "<input type='submit' value='Delete post' name='$this->getDeletePost' class='deletePost' />";
				$ret .= "<input type='hidden' value='" . $post->GetId() ."' name='$this->getPostId' readonly='readonly' /></form>";			
			}	
					 			 
		}
		
		return $ret;
	}

	//Retunerar lista av Post-objekt via medskickad array.
	public function RenderMyPostList(\Model\PostArray $postArray)
	{
		$ret = NULL;
					
		foreach ($postArray->Get() as $post)
		{
			$topicLink = NavigationView::getTopicLinkByPost($post);
			$ret .= "<div class='myPosts'><a href='$topicLink' class='listHeader'>Re: " . $post->GetTopicHeader() . "</a>
					 <p class='nameAndDate'>By " . $post->GetUserName() . " " . $post->GetDate() . "</p></div>";				 
		}
		
		return $ret;
	}	

	//Retunerar formulär för skapande av ny post	
	public function RenderNewPostForm()
	{
	    return $newTopicForm = 

	    "<form name='frmNewPost' id='frmNewPost' method='post'>	    		
			<div class='contentHeader'><h2>New post</h2></div>							
			<textarea type='text' name='$this->getContent' id='newPostContentBox'></textarea> 			
			<input type='submit' value='Post' name='$this->getCreateKey' class='button' />				  			
		</form>";		
	}	
	
	//Retunerar Post-rubriken, annars NULL
	public function GetHeader() 
	{	  	
		if (isset($_POST[$this->getHeader]) == TRUE)
		{				 		
			return $_POST[$this->getHeader];
		}
		
		return NULL;		
	}
	
	//Retunerar post-innehåll, annars NULL
	public function GetContent() 
	{	
		if (isset($_POST[$this->getContent]) == TRUE)
		{
			return $_POST[$this->getContent];
		}
				
		return NULL;			
	}	

	//Retunerar post-id, annars FALSE
	public function GetPostId() 
	{
		if (isset($_POST[$this->getPostId]))
		{
			return $_POST[$this->getPostId];	
		}	
		
		return FALSE;	
	}	

	//Retunerar TRUE om anv har tryckt på "Post", annars FALSE	
	public function TriedToCreatePost() 
	{	
		if (isset($_POST[$this->getCreateKey]) == TRUE)
		{
			unset( $_POST[$this->getCreateKey] );
			return TRUE;
		}
				
		return FALSE;			
	}	

	//Retunerar TRUE om anv har tryckt på "Delete Post", annars FALSE		
	public function TriedToDeletePost() 
	{	
		if (isset($_POST[$this->getDeletePost]) == TRUE)
		{
			return TRUE;
		}
				
		return FALSE;			
	}		
		
}


?>